/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.openbrew.openbrewrf.Administration;
import com.openbrew.openbrewrf.Brews;
import com.openbrew.openbrewrf.views.BrewView;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import tools.BrewsTools;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.brews")
public class BrewsFacadeREST extends AbstractFacade<Brews> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public BrewsFacadeREST() {
        super(Brews.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public Brews create(@WebParam(name = "brews") Brews entity) {
        entity = super.create(entity);
        return entity;
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Brews entity) {
        super.edit(entity);
    }
    
    @PUT
    @Path("processBrewUpdate")
    @Consumes({"application/xml", "application/json"})
    public void processBrewUpdate(Brews entity) {
        //Modify inventories first.
        final BrewsTools bt = new BrewsTools();
        bt.doBrewUpdate(em, entity);
        em.getEntityManagerFactory().getCache().evict(BrewView.class);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Brews find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Brews> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Brews> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("namedQueryRange/{from}/{to}/{str}/{id}")
    @Produces({"application/xml", "application/json"})
    public List<Brews> findNamedQueryRange(@PathParam("from") Integer from
            , @PathParam("to") Integer to, @PathParam("str") String str
            , @PathParam("id") Integer id) {
        return super.findNamedQueryRange(new int[]{from, to}, str, id);
    }

    @GET
    @Path("namedQueryCount/{str}/{id}")
    @Produces("text/plain")
    public String namedQueryCount(@PathParam("str") String str
            , @PathParam("id") Integer id) {
        return String.valueOf(super.namedQueryCount(str, id, "cId"));
    }
    
    @GET
    @Path("runNamedQueryWithID/{str}/{id}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Brews> runNamedQueryWithID(@PathParam("str") String str
            , @PathParam("id") int id){
        return super.runNamedQueryWithID(str, id);
    }
    
    @GET
    @Path("runNamedQuery/{str}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Brews> runNamedQuery(@PathParam("str") String str){
        return super.runNamedQuery(str);
    }
    
    @GET
    @Path("getSplittableBatches")
    @Produces({"application/xml", "application/json"})
    public List<Brews> getSplittableBatches(@PathParam("str") String str){
        final Administration admin = getAdmin(em);
        final List<Brews> tempBrews = getPackageableBrews(em);
        List<Brews> selBrews = new ArrayList<Brews>();
        for(final Brews brew : tempBrews){
            if(brew.getVolLeft()>(admin.getSystemSize()+2)){
                selBrews.add(brew);
            }
        }
        return selBrews;
    }
    
    private Administration getAdmin(final EntityManager en){
        TypedQuery<Administration> query = en.createQuery(
            "SELECT a FROM Administration a WHERE a.aNum = '1'", Administration.class);
        Administration admin = query.getSingleResult();
        return admin;
    }
    
    private List<Brews> getPackageableBrews(final EntityManager en){
        TypedQuery<Brews> query = en.createNamedQuery("Brews.findInProcess", Brews.class);
        return query.getResultList();
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    @GET
    @Path("runQuery/{str}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Brews> runQuery(@PathParam("str") String str){
        return super.runQuery(str);
    }

    @PUT
    @Path("doBatchSplit/{vesselID}/{beerID}/{volume}")
    @Produces({"application/xml", "application/json"})
    public Brews doBatchSplit(@WebParam(name = "brews") Brews entity
            , @PathParam("vesselID") Integer vesselID, @PathParam("beerID") Integer beerID
            , @PathParam("volume") float volume) {
        //Modify inventories first.
        BrewsTools bt = new BrewsTools();
        return bt.doBatchSplit(em, entity, vesselID, beerID, volume);
    }
    
}
